package com.czxy.controller;


import com.czxy.domain.User;
import com.czxy.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;


    @GetMapping
    public ResponseEntity findAll() {

        try {


            List<User> list = userService.findAll();

            int i = 1 / 0;
            return ResponseEntity.ok(list);


        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }


    @GetMapping("/{id}/{name}")
    public ResponseEntity findByUid(@PathVariable("id") Integer uid, @PathVariable("name") String loginName) {

        try {

            List<User> user = userService.findById(uid);

            return ResponseEntity.ok(user);

        } catch (Exception e) {

            e.printStackTrace();

            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);


        }
    }


    @PostMapping
    public ResponseEntity add(@RequestBody  User user) {
        try {

            userService.add(user);

            return new ResponseEntity(HttpStatus.CREATED);
        } catch (Exception e) {
            e.printStackTrace();

            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);

        }
    }


    @PutMapping
    public  ResponseEntity update(@RequestBody User user){

        try{
            userService.updateById(user);

            return  new ResponseEntity(HttpStatus.NO_CONTENT);
        }catch (Exception e){
            e.printStackTrace();

            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }





}




























